package com.ivo.jersey;

import javax.naming.directory.SearchResult;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import com.ivo.jersey.pojo.Task;
import com.ivo.jersey.taskMgr.TaskMgr;
import com.ivo.jersey.taskMgr.impl.TaskMgrImpl;

@Path("/tasks/")
public class TaskResource {

	private TaskMgr mgr = new TaskMgrImpl();

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/tasks/")
	public Response searchAllTasks() {
		SearchResult tasks = mgr.serachAllTasks();
		System.out.println("searchAllTasks, TODO");

		return Response.status(Status.OK).entity(tasks).build();
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/tasks/{taskID}")
	public Response searchTasks(@PathParam("taskID") String taskID) {
		Task task = mgr.getTask(taskID);
		System.out.println("searchTasks:[" + taskID + "]");
		return Response.status(Status.OK).entity(task).build();
	}

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@Path("/tasks/")
	public Response createTask(Task task) {
		String taskID = mgr.createTask(task);
		String jsonStrng = "{\"taskID\":\"" + taskID + "\"}";
		System.out.println("createTask:[" + taskID + "][" + jsonStrng + "]");
		return Response.status(Status.OK).entity(jsonStrng).build();
	}

	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/tasks/{taskID}")
	public void updateTask(@PathParam("taskID") String taskID, Task task) {
		System.out.println("updateTask:[" + taskID + "][" + taskID + "]");
		mgr.updateTask(taskID, task);
	}

	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/tasks/{taskID}")
	public void deleteTask(@PathParam("taskID") String taskID) {
		System.out.println("deleteTask:[" + taskID + "][" + taskID + "]");
		mgr.deleteTask(taskID);
	}

	public TaskMgr getMgr() {
		return mgr;
	}

	public void setMgr(TaskMgr mgr) {
		this.mgr = mgr;
	}

}
